(function(){BX.namespace("BX.Numerator");BX.Numerator=function(e){this.isSlider=e.isSlider;this.isEdit=e.isEdit;this.isMultipleSequences=e.isMultipleSequences;this.defaultDelimiter=e.defaultDelimiter;this.roles={templateInput:"numerator-template-input",btnSave:"btn-save",btnCancel:"btn-cancel",form:"numerator-edit-form",wordBtn:"numerator-template-word-btn",wordText:"template-word-text",timezoneToggle:"numerator-timezoneToggle",showSetNextNumberToggle:"numerator-set-next-number-toggle",timezones:"numerator-timezone",sequenceBlock:"nextNumberForSequence-wrapper",nameInput:"numerator-name-input",error:"numerator-error",startBlockWrapper:"start-wrapper",periodSelect:"numerator-periodicBy-select",timezoneSelect:"numerator-timezone-select",wordBtnWrapper:"numerator-edit-word-btn-wrapper"};this.templateInput=this.selectByRoles(this.roles.templateInput);this.helpArticleToggles=this.selectByRoles("help-article-toggle","all");this.wordBtnWrapper=this.selectByRoles(this.roles.wordBtnWrapper);this.startBlockWrapper=this.selectByRoles(this.roles.startBlockWrapper);this.hiddenTemplateInput=this.selectByRoles("numerator-hidden-template-input");this.periodSelect=this.selectByRoles(this.roles.periodSelect);this.timezoneSelect=this.selectByRoles(this.roles.timezoneSelect);this.timezoneToggle=this.selectByRoles(this.roles.timezoneToggle);this.showSetNextNumberToggle=this.selectByRoles(this.roles.showSetNextNumberToggle);this.timezones=this.selectByRoles(this.roles.timezones);this.sequenceBlock=this.selectByRoles(this.roles.sequenceBlock);this.saveButton=this.selectByRoles(this.roles.btnSave);this.cancelButton=this.selectByRoles(this.roles.btnCancel);this.numeratorForm=this.selectByRoles(this.roles.form);this.nameInput=this.selectByRoles(this.roles.nameInput);this.settingsBlock={};this.errorBlock=null;this.errors=[];this.errorMessages=e&&e.errors?e.errors:{};this.wordButtons=this.selectByRoles(this.roles.wordBtn,"all");for(var t=0;t<this.wordButtons.length;t++){var i=this.selectByRoles("settings-type-"+this.wordButtons[t].dataset["type"]);if(i){this.settingsBlock[this.wordButtons[t].dataset["type"]]=i}}this.hideInitial();this.addEventHandlers();this.fillTemplate();this.updateTemplateHiddenInput();BX.UI.Hint.init(this.selectByRoles("numerator-container"))};BX.Numerator.prototype={addEventHandlers:function(){BX.bind(this.saveButton,"click",BX.delegate(this.onSaveClick,this));BX.bind(this.cancelButton,"click",BX.delegate(this.onCancelClick,this));BX.bind(this.templateInput,"keyup",BX.delegate(this.handleKeyPress,this));for(var e=0;e<this.wordButtons.length;e++){BX.bind(this.wordButtons[e],"click",BX.delegate(this.onTemplateWordBtnClick,this))}BX.bind(this.periodSelect,"change",BX.delegate(this.onPeriodOptionClick,this));BX.bind(this.timezoneToggle,"click",BX.delegate(this.onTimezoneToggleClick,this));BX.bind(this.showSetNextNumberToggle,"click",BX.delegate(this.onShowSetNextNumberToggleClick,this));for(var e=0;e<this.wordButtons.length;e++){BX.bind(this.helpArticleToggles[e],"click",BX.delegate(this.onHelpArticleToggleClick,this))}},hideInitial:function(){this.hideElement(this.timezones);if(this.sequenceBlock){this.hideElement(this.sequenceBlock)}if(!this.periodSelect.value){this.hideElement(this.timezoneToggle)}else if(this.timezoneSelect.value){this.showElement(this.timezones)}if(this.isEdit&&!this.periodSelect.value){this.hideElement(this.startBlockWrapper)}if(!this.showSetNextNumberToggle&&!this.sequenceBlock){this.showElement(this.startBlockWrapper)}},onPeriodOptionClick:function(e){if(this.periodSelect.value){this.showElement(this.timezoneToggle);if(this.isEdit){this.showElement(this.startBlockWrapper)}if(this.timezoneSelect.value){this.showElement(this.timezones)}}else{this.hideElement(this.timezoneToggle);this.hideElement(this.timezones);if(this.isEdit){this.hideElement(this.startBlockWrapper)}}},fillTemplate:function(){var e;var t=e=this.templateInput.dataset["value"];if(t){var i=t.match(/{[a-z0-9_:!@#%^&\(*;\)]+?}/gi);if(i){for(var s=0;s<i.length;s++){var r=i[s];var n=this.selectByRoles([[this.roles.wordBtn,""],[r,"","word"]]);if(n){this.activateButton(n);this.changeSettingsVisibility(n.dataset["type"]);e=this.appendTextToTemplate(r,e);this.templateInput.appendChild(this.getTemplateWordNode(n.dataset["type"],r))}}}if(e){this.templateInput.appendChild(document.createTextNode(e))}}else{var l="{NUMBER}";var o=this.selectByRoles([[this.roles.wordBtn,""],[l,"","word"]]);if(o){this.activateButton(o);this.changeSettingsVisibility(o.dataset["type"]);this.templateInput.appendChild(this.getDefaultDelimiterNode());this.templateInput.appendChild(this.getTemplateWordNode(o.dataset["type"],l))}this.deleteFirstDelimiter()}},onHelpArticleToggleClick:function(e){top.BX.Helper.show("redirect=detail&code=7486453")},onShowSetNextNumberToggleClick:function(e){var t=this.selectByRoles("numerator-hidden-id-input");if(t&&t.value&&this.isMultipleSequences){var i=BX.util.add_url_param("/bitrix/components/bitrix/main.numerator.edit.sequence/slider.php",{NUMERATOR_ID:t.value});BX.SidePanel.Instance.open(i,{width:650,cacheable:false})}else if(this.sequenceBlock){if(this.isVisibleElement(this.sequenceBlock)){this.hideElement(this.sequenceBlock)}else{this.showElement(this.sequenceBlock)}}},onTimezoneToggleClick:function(e){if(this.isVisibleElement(this.timezones)){this.hideElement(this.timezones)}else{this.showElement(this.timezones)}},checkForErrors:function(){if(this.nameInput&&!(this.nameInput.value&&this.nameInput.value.trim()!=="")){this.errors.push({field:this.nameInput,text:this.errorMessages&&this.errorMessages.emptyField?this.errorMessages.emptyField:""})}if(!(this.templateInput.innerText&&this.templateInput.innerText.trim()!=="")){this.errors.push({field:this.templateInput,text:this.errorMessages&&this.errorMessages.emptyField?this.errorMessages.emptyField:""})}},clearErrors:function(){this.errors=[];var e=this.selectByRoles("numerator-error-block","all");for(var t=e.length-1;t>=0;t--){e[t].parentNode.removeChild(e[t])}var i=document.querySelectorAll(".main-numerator-edit-input-alert");for(var s=0;s<i.length;s++){i[s].classList.remove("main-numerator-edit-input-alert")}},showErrors:function(){let e=[];if(!this.errorBlock){this.errorBlock=BX.create("div",{attrs:{class:"main-numerator-edit-alert-text"},dataset:{role:"numerator-error-block"},text:null})}this.numeratorForm.prepend(this.errorBlock);for(let t=0;t<this.errors.length;t++){let i=this.errors[t];e.push(i.text);if(!i.field){i.field=this.numeratorForm}if(!i.field.classList.contains("main-numerator-edit-input-alert")){i.field.classList.add("main-numerator-edit-input-alert")}}this.errorBlock.innerHTML=e.join("<br>")},updateErrors:function(){this.clearErrors();this.checkForErrors();this.showErrors()},closeSlider:function(){if(this.isSlider){var e=BX.SidePanel.Instance.getTopSlider();if(e){e.close()}}},onCancelClick:function(e){e.preventDefault();this.closeSlider()},onSaveClick:function(e){e.stopPropagation();e.preventDefault();this.updateErrors();if(this.errors&&this.errors.length){return}var t=new FormData(this.numeratorForm);BX.ajax.runAction("main.api.numerator.save",{data:t}).then(function(e){BX.SidePanel.Instance.postMessageAll(window,"numerator-saved-event",{id:e.data.id,name:this.nameInput?this.nameInput.value:"",template:this.hiddenTemplateInput?this.hiddenTemplateInput.value:"",type:e.data.type});this.closeSlider()}.bind(this),function(e){this.errors=[];for(var t=0;t<e.errors.length;t++){this.errors.push({text:e.errors[t].message})}this.showErrors()}.bind(this))},updateTemplateHiddenInput:function(){this.hiddenTemplateInput.value=this.templateInput.innerText},handleKeyPress:function(e){this.updateTemplateHiddenInput();if(e.keyCode===8||e.keyCode===46){for(var t in this.settingsBlock){this.hideSettingsBlockByType(t)}for(var i=0;i<this.wordButtons.length;i++){this.disActivateButton(this.wordButtons[i])}var s=this.selectByRoles([this.roles.templateInput,this.roles.wordText],"all");for(var r=0;r<s.length;r++){var n=s[r];var l=this.settingsBlock[n.dataset["settingsType"]];this.activateButton(this.selectByRoles([[this.roles.wordBtn,""],[n.dataset["word"],"","word"]]));this.showElement(l)}this.deleteFirstDelimiter()}},onTemplateWordBtnClick:function(e){e.preventDefault();e.stopPropagation();var t=e.currentTarget;if(this.isActiveBtn(t)){this.disActivateButton(t);this.removeWordsFromTemplate(t.dataset["word"]);this.hideSettingsBlockByType(t.dataset["type"])}else{this.activateButton(t);this.insertWordIntoTemplate(t.dataset["type"],t.dataset["word"]);this.changeSettingsVisibility(t.dataset["type"])}this.deleteFirstDelimiter();this.updateTemplateHiddenInput()},deleteFirstDelimiter:function(){if(this.templateInput.children&&this.templateInput.children.length&&this.templateInput.children[0].dataset&&this.templateInput.children[0].dataset.role==="default-delimiter"){this.templateInput.children[0].parentNode.removeChild(this.templateInput.children[0])}},hideSettingsBlockByType:function(e){this.hideElement(this.settingsBlock[e])},getDefaultDelimiterNode:function(){return BX.create("span",{text:this.defaultDelimiter,dataset:{role:"default-delimiter"}})},insertWordIntoTemplate:function(e,t){var i=this.getTemplateWordNode(e,t);var s=window.getSelection();if(s.getRangeAt&&s.rangeCount){var r=s.getRangeAt(0);if(this.elementContainsSelection(this.templateInput)){r.insertNode(this.getDefaultDelimiterNode());r.collapse(false);r.insertNode(i)}else{this.templateInput.appendChild(this.getDefaultDelimiterNode());this.templateInput.appendChild(i)}r.collapse(false)}else{this.templateInput.appendChild(this.getDefaultDelimiterNode());this.templateInput.appendChild(i)}},getTemplateWordNode:function(e,t){var i=BX.create("span",{props:{className:"main-numerator-edit-template-word"},dataset:{settingsType:e,word:t,role:this.roles.wordText},text:t});i.setAttribute("contenteditable",false);return BX.create("span",{props:{className:"main-numerator-edit-template-word-wrap"},events:{keyup:BX.delegate(this.handleKeyPress,this)},children:[i]})},changeSettingsVisibility:function(e){if(this.templateInput){var t=this.settingsBlock[e];if(t&&!this.isVisibleElement(t)){this.showElement(t);var i=t;var s=i.parentNode;var r=s.removeChild(i);s.insertBefore(r,s.childNodes[0])}}},appendTextToTemplate:function(e,t){var i=t.substring(0,t.indexOf(e));if(i){this.templateInput.appendChild(document.createTextNode(i))}return t.replace(i+e,"")},removeWordsFromTemplate:function(e){var t=this.selectByRoles([[this.roles.templateInput],[this.roles.wordText,""],[e,"","word"]],"all");for(var i=0;i<t.length;i++){var s=t[i];if(s.previousSibling&&s.previousSibling.dataset&&s.previousSibling.dataset.role==="default-delimiter"){s.previousSibling.parentNode.removeChild(s.previousSibling)}if(s.parentNode.previousSibling&&s.parentNode.previousSibling.dataset&&s.parentNode.previousSibling.dataset.role==="default-delimiter"){s.parentNode.previousSibling.parentNode.removeChild(s.parentNode.previousSibling)}var r=s.parentNode;if(r){r.removeChild(s)}if(r.classList.contains("main-numerator-edit-template-word-wrap")&&r.children.length===0){r.parentNode.removeChild(r)}}},isActiveBtn:function(e){if(e){return e.classList.contains("main-numerator-edit-template-word-btn-clicked")}},selectByRoles:function(e,t){var i="";if(!Array.isArray(e)){e=[e]}for(var s=0;s<e.length;s++){if(Array.isArray(e[s])){i+="[data-"+(e[s][2]?e[s][2]:"role")+'="'+e[s][0]+'"]'+(e[s][1]===""?"":" ");continue}i+='[data-role="'+e[s]+'"] '}return t?document.querySelectorAll(i):document.querySelector(i)},isVisibleElement:function(e){if(e){return e.classList.contains("main-numerator-edit-hide")===false}},showElement:function(e){if(e){e.classList.remove("main-numerator-edit-hide")}},hideElement:function(e){if(e){e.classList.add("main-numerator-edit-hide")}},disActivateButton:function(e){if(e){e.classList.remove("main-numerator-edit-template-word-btn-clicked")}},activateButton:function(e){if(e){e.classList.add("main-numerator-edit-template-word-btn-clicked")}},isOrContains:function(e,t){var i=10;var s=0;while(e){s++;if(s>i){break}if(e===t){return true}e=e.parentNode}return false},elementContainsSelection:function(e){var t;if(window.getSelection){t=window.getSelection();if(t.rangeCount>0){for(var i=0;i<t.rangeCount;++i){if(!this.isOrContains(t.getRangeAt(i).commonAncestorContainer,e)){return false}}return true}}else if((t=document.selection)&&t.type!=="Control"){return this.isOrContains(t.createRange().parentElement(),e)}return false}}})();
//# sourceMappingURL=script.map.js